package com.cslg.questionnaires.controller;

import com.cslg.questionnaires.entity.Student;
import com.cslg.questionnaires.entity.Teacher;
import com.cslg.questionnaires.service.PreViewQuestionFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
public class PreViewQuestionFormController {
    @Autowired
    private PreViewQuestionFormService preViewQuestionFormService;

    @GetMapping("/PreViewQuestionForm/{questionFormId}")
    public ModelAndView getPreViewFormPage(@PathVariable int questionFormId, HttpSession session){
        String userType = (String) session.getAttribute("userType");
        int userid;
        if (userType.equals("Teacher")){
            Teacher teacher = (Teacher) session.getAttribute("user");
            userid = teacher.getId();
        } else {
            Student student = (Student) session.getAttribute("user");
            userid = student.getId();
        }
        HashMap<String, Object> msg = preViewQuestionFormService.getPreViewQuestion(questionFormId,userid,userType);
        if ((int)msg.get("code") == 200){
            return new ModelAndView("PreViewQuestionForm",msg);
        }else{
            return new ModelAndView("QuestionFormError",msg);
        }
    }
}
